package com.songlanyun.modules.publicbenefitmsg.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.enums.DonationConstant;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;
import com.songlanyun.common.utils.custom.ClassConvertUtils;
import com.songlanyun.modules.account.service.UserInfoService;
import com.songlanyun.modules.donation.entity.DonationEntity;
import com.songlanyun.modules.donation.service.DonationService;
import com.songlanyun.modules.donationpoints.entity.DonationPointsEntity;
import com.songlanyun.modules.donationpoints.service.DonationPointsService;
import com.songlanyun.modules.officialDonation.entity.OfficialDonationEntity;
import com.songlanyun.modules.officialDonation.service.OfficialDonationService;
import com.songlanyun.modules.publicbenefitmsg.dao.PublicBenefitMsgDao;
import com.songlanyun.modules.publicbenefitmsg.entity.PublicBenefitMsgEntity;
import com.songlanyun.modules.publicbenefitmsg.model.DonateMsg;
import com.songlanyun.modules.publicbenefitmsg.model.IPublicBenefitMsgBody;
import com.songlanyun.modules.publicbenefitmsg.model.to.PublicBenefitMsgTO;
import com.songlanyun.modules.publicbenefitmsg.service.PublicBenefitMsgService;
import com.songlanyun.modules.rabbitmq.MqConstant;
import com.songlanyun.modules.rabbitmq.service.SendMsgService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;


@Service("publicBenefitMsgService")
public class PublicBenefitMsgServiceImpl extends ServiceImpl<PublicBenefitMsgDao, PublicBenefitMsgEntity> implements PublicBenefitMsgService {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private SendMsgService sendMsgService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Long userId = MapUtil.getLong(params, "userId");
        Integer type = MapUtil.getInt(params, "type");

        IPage<PublicBenefitMsgEntity> page = this.page(
                new Query<PublicBenefitMsgEntity>().getPage(params),
                new LambdaQueryWrapper<PublicBenefitMsgEntity>()
                        .eq(ObjectUtil.isNotNull(userId), PublicBenefitMsgEntity::getUserId, userId)
                        .eq(ObjectUtil.isNotNull(type), PublicBenefitMsgEntity::getType, type)
                        .orderByDesc(PublicBenefitMsgEntity::getCreateTime)
        );
        //加载消息体
        page.getRecords().forEach(PublicBenefitMsgEntity::loadMsgBody);

        return new PageUtils(page);
    }

    @Override
    public void add(Long userId, DonationConstant.MsgType type, IPublicBenefitMsgBody msgBody, String content) {
        //真实姓名脱敏
        String realName = userInfoService.getResNameById(userId);
        PublicBenefitMsgEntity entity = new PublicBenefitMsgEntity(userId, type, msgBody, realName + content);

        this.baseMapper.insert(entity);
    }

    @Override
    public void sendMsg(PublicBenefitMsgTO to) {
        sendMsgService.sendCommon(MqConstant.PublicBenefitMsgConstant.PUBLIC_BENEFIT_MSG_EXCHANGE,
                MqConstant.PublicBenefitMsgConstant.PUBLIC_BENEFIT_MSG_ROUTING
                , ClassConvertUtils.transformJSONToSnake(to));
    }


}
